利用Python进行数据可视化常见的9种方法!超实用! 您所在的位置:网站首页 python excel数据可视化 利用Python进行数据可视化常见的9种方法!超实用!

利用Python进行数据可视化常见的9种方法!超实用!

#利用Python进行数据可视化常见的9种方法!超实用!| 来源: 网络整理| 查看: 265

前言

如同艺术家们用绘画让人们更贴切的感知世界,数据可视化也能让人们更直观的传递数据所要表达的信息。

我们今天就分享一下如何用 Python 简单便捷的完成数据可视化。

其实利用 Python 可视化数据并不是很麻烦,因为 Python 中有两个专用于可视化的库 matplotlib 和 seaborn 能让我们很容易的完成任务。

Matplotlib:基于Python的绘图库,提供完全的 2D 支持和部分 3D 图像支持。在跨平台和互动式环境中生成高质量数据时,matplotlib 会很有帮助。也可以用作制作动画。 Seaborn:该 Python 库能够创建富含信息量和美观的统计图形。Seaborn 基于 matplotlib,具有多种特性,比如内置主题、调色板、可以可视化单变量数据、双变量数据,线性回归数据和数据矩阵以及统计型时序数据等,能让我们创建复杂的可视化图形。

我们用 Python 可以做出哪些可视化图形?

那么这里可能有人就要问了,我们为什么要做数据可视化?比如有下面这个图表:

当然如果你把这张图表丢给别人,他们倒是也能看懂,但无法很直观的理解其中的信息,而且这种形式的图表看上去也比较 low,这个时候我们如果换成直观又美观的可视化图形,不仅能突显逼格,也能让人更容易的看懂数据。

下面我们就用上面这个简单的数据集作为例子,展示用 Python 做出9种可视化效果,并附有相关代码。

导入数据集

import matplotlib.pyplot as plt import pandas as pd df=pd.read_excel("E:/First.xlsx", "Sheet1")

可视化为直方图

fig=plt.figure() #Plots in matplotlib reside within a figure object, use plt.figure to create new figure #Create one or more subplots using add_subplot, because you can't create blank figure ax = fig.add_subplot(1,1,1) #Variable ax.hist(df['Age'],bins = 7) # Here you can play with number of bins Labels and Tit plt.title('Age distribution') plt.xlabel('Age') plt.ylabel('#Employee') plt.show()

可视化为箱线图

import matplotlib.pyplot as plt import pandas as pd fig=plt.figure() ax = fig.add_subplot(1,1,1) #Variable ax.boxplot(df['Age']) plt.show()

可视化为小提琴图

import seaborn as sns sns.violinplot(df['Age'], df['Gender']) #Variable Plot sns.despine()

可视化为条形图

var = df.groupby('Gender').Sales.sum() #grouped sum of sales at Gender level fig = plt.figure() ax1 = fig.add_subplot(1,1,1) ax1.set_xlabel('Gender') ax1.set_ylabel('Sum of Sales') ax1.set_title("Gender wise Sum of Sales") var.plot(kind='bar')

可视化为折线图

var = df.groupby('BMI').Sales.sum() fig = plt.figure() ax1 = fig.add_subplot(1,1,1) ax1.set_xlabel('BMI') ax1.set_ylabel('Sum of Sales') ax1.set_title("BMI wise Sum of Sales") var.plot(kind='line')

可视化为堆叠柱状图

var = df.groupby(['BMI','Gender']).Sales.sum() var.unstack().plot(kind='bar',stacked=True, color=['red','blue'], grid=False)

可视化为散点图

fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.scatter(df['Age'],df['Sales']) #You can also add more variables here to represent color and size. plt.show()

可视化为泡泡图

fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.scatter(df['Age'],df['Sales'], s=df['Income']) # Added third variable income as size of the bubble plt.show()

可视化为饼状图

var=df.groupby(['Gender']).sum().stack() temp=var.unstack() type(temp) x_list = temp['Sales'] label_list = temp.index pyplot.axis("equal") #The pie chart is oval by default. To make it a circle use pyplot.axis("equal") #To show the percentage of each pie slice, pass an output format to the autopctparameter plt.pie(x_list,labels=label_list,autopct="%1.1f%%") plt.title("Pastafarianism expenses") plt.show()

可视化为热度图

import numpy as np #Generate a random number, you can refer your data values also data = np.random.rand(4,2) rows = list('1234') #rows categories columns = list('MF') #column categories fig,ax=plt.subplots() #Advance color controls ax.pcolor(data,cmap=plt.cm.Reds,edgecolors='k') ax.set_xticks(np.arange(0,2)+0.5) ax.set_yticks(np.arange(0,4)+0.5) # Here we position the tick labels for x and y axis ax.xaxis.tick_bottom() ax.yaxis.tick_left() #Values against each labels ax.set_xticklabels(columns,minor=False,fontsize=20) ax.set_yticklabels(rows,minor=False,fontsize=20) plt.show()

你也可以自己试着根据两个变量比如性别(X 轴)和 BMI(Y 轴)绘出热度图。

结语

本文我们分享了如何利用 Python 及 matplotlib 和 seaborn 库制作出多种多样的可视化图形。通过上面的例子,我们应该可以感受到利用可视化能多么美丽的展示数据。而且和其它语言相比,使用 Python 进行可视化更容易简便一些。

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

参考资料:

https://www.analyticsvidhya.com/blog/2015/05/data-visualization-python/

您可能感兴趣的文章:Python数据可视化 pyecharts实现各种统计图表过程详解利用Python代码实现数据可视化的5种方法详解Python数据可视化库seaborn的使用总结Python数据可视化:箱线图多种库画法Python数据可视化之画图Python数据可视化:饼状图的实例讲解Python数据可视化:泊松分布详解Python数据可视化:幂律分布实例详解python实现股票历史数据可视化分析案例Python "手绘风格"数据可视化方法实例汇总


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有